<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>1.字符串形式的ref</title>
    <!-- 引入react核心库 -->
    <script src="../React-js/react.development.js"></script>
    <!-- 引入react-dom,用于支持react操作DOM -->
    <script
      type="text/javascript"
      src="../React-js/react-dom.development.js"
    ></script>
    <!-- 引入babel,用于将jsx转换为js -->
    <script type="text/javascript" src="../React-js/babel.min.js"></script>

    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      body {
        display: flex;
        justify-content: center;
        align-items: center;
        min-height: 100vh;
        background-color: #303030;
      }
      .demo {
        display: flex;
        gap: 20px;
      }
      input {
        border: none;
        outline: none;
        padding: 10px;
        font-size: 18px;
        border-radius: 5px;
      }
      button {
        padding: 10px;
        border-radius: 10px;
        font-size: 18px;
        cursor: pointer;
      }
      button:active {
        transform: scale(0.98);
      }
    </style>
  </head>
  <body>
    <!-- 准备好一个“容器” -->
    <div id="test"></div>
    <script type="text/babel">
      // 1.创建组件
      class MyComponent extends React.Component {
        showData = () => {
          const { inputEl } = this.refs;
          alert(inputEl.value);
        };
        // 右侧 失去焦点
        blurData = () => {
          const { inputBlur } = this.refs;
          alert(inputBlur.value);
        };
        render() {
          return (
            <div className="demo">
              <input ref="inputEl" type="text" placeholder="点击按钮提示数据" />
              <button ref="btn" onClick={this.showData}>
                点我提示左边数据
              </button>
              <input
                onBlur={this.blurData}
                ref="inputBlur"
                type="text"
                placeholder="失去焦点提示数据"
              />
            </div>
          );
        }
      }
      // 2.渲染组件到页面
      ReactDOM.render(<MyComponent />, document.getElementById('test'));
    </script>
  </body>
</html>
